﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cmsservice.Entity;
using cmsservice.DAL;
using cmsservice.Common;
using System.Text.RegularExpressions;
using cmsservice.Common.Memcached;

namespace cmsservice.BLL
{
    public class Em_Grab_GaoTime_ZxBLL
    {
        public List<Em_Grab_GaoTime_Zx> Get新闻评论(string stock)
        {
            Em_Grab_GaoTime_ZxDAL dal = new Em_Grab_GaoTime_ZxDAL();
            List<Em_Grab_GaoTime_Zx> lst2 = dal.Get新闻评论(stock, 0, false);
            List<Em_Grab_GaoTime_Zx> lst = new List<Em_Grab_GaoTime_Zx>();
            var lst3 = dal.Get新闻评论New(stock, 0, false);
            Add(lst, lst2);
            Add(lst, lst3);
            return lst.Take(200).OrderByDescending(x=>x.ReportTime).ToList();
        }


        public List<Em_Grab_GaoTime_Zx> Get行业新闻(string stock)
        {
            Em_Grab_GaoTime_ZxDAL dal = new Em_Grab_GaoTime_ZxDAL();
            List<Em_Grab_GaoTime_Zx> lst = new List<Em_Grab_GaoTime_Zx>();
            BlockRStockBLL blockRStockBLL=new BlockRStockBLL ();
            var bk = blockRStockBLL.GetBKByStock(stock);
            if (bk == null)
            {
                return new List<Em_Grab_GaoTime_Zx>();
            }
            var lst2 = dal.Get行业新闻_bk(bk.BlockCode, 0, false);
            Add(lst, lst2);
            var lst3 = dal.Get行业新闻(stock, 0, false);
            Add(lst, lst3);
            return lst.OrderByDescending(x=>x.ReportTime).Take(200).ToList();
        }

        public List<Em_Grab_GaoTime_Zx> Get行业公司动态(string stock)
        {
            Em_Grab_GaoTime_ZxDAL dal = new Em_Grab_GaoTime_ZxDAL();
            List<Em_Grab_GaoTime_Zx> lst = new List<Em_Grab_GaoTime_Zx>();
            BlockRStockBLL blockRStockBLL=new BlockRStockBLL ();
            var stocks = blockRStockBLL.GetBlockByStock(stock);
            var lst2 = dal.Get行业公司动态(stocks, 0, false);
            Add(lst, lst2);
            return lst.Take(200).ToList();
        }

        public List<Em_Grab_GaoTime_Zx> Get股票分类报价New(string stock)
        {
            Em_Grab_GaoTime_ZxDAL dal = new Em_Grab_GaoTime_ZxDAL();
            List<Em_Grab_GaoTime_Zx> lst = new List<Em_Grab_GaoTime_Zx>();
            var lst0 = dal.Get新闻评论(stock, 10, true);
            Add(lst, lst0);
            var lst1 = dal.Get新闻评论New(stock, 10, true);
            Add(lst, lst1);
            var lst2 = dal.Get行业新闻(stock, 10, true);
            Add(lst, lst2);
            var lst4 = Get公告列表_New(stock, 10, true, true);
            Add(lst, lst4);
            return lst.OrderByDescending(x => x.ReportTime).Take(10).ToList();
        }

        public List<Em_Grab_GaoTime_Zx> Get股票分类报价(string stock)
        {
            Em_Grab_GaoTime_ZxDAL dal = new Em_Grab_GaoTime_ZxDAL();
            List<Em_Grab_GaoTime_Zx> lst = new List<Em_Grab_GaoTime_Zx>();
            var lst1 = dal.Get新闻评论(stock, 10, true);
            Add(lst, lst1);
         
            var lst2 = dal.Get行业新闻(stock, 10, true);
            Add(lst, lst2);
            var ggLst = Get公告列表_New(stock);
            var lst4 = Get公告列表_New(stock, 10, true, true);
            Add(lst, lst4);
            return lst.OrderByDescending(x => x.ReportTime).Take(10).ToList();
        }

        void Add(List<Em_Grab_GaoTime_Zx> lst1, List<Em_Grab_GaoTime_Zx> lst2)
        {
            foreach (var c in lst2)
            {
                if (!lst1.Any(x => x.Title == c.Title))
                {
                    if (!string.IsNullOrEmpty(c.Title) )
                    {
                        c.Title = StringTools.TxtClear(c.Title);
                        //if (RegexTools.StartWith(c.Title,"潜力股推荐")
                        //    || RegexTools.StartWith(c.Title,"个股推荐")
                        //    || RegexTools.Contains(c.Title, "维持推荐")
                        //    || RegexTools.Contains(c.Title, "积极推荐")
                        //    || RegexTools.Contains(c.Title, "\"[\\s\\S]*推荐\\s\\S]*\"")
                        //    || RegexTools.Contains(c.Title, "推荐评级")
                        //    || RegexTools.Contains(c.Title, " [\\s\\S]*推荐")
                        //    || RegexTools.Contains(c.Title, "强烈推荐")
                        //    || RegexTools.Contains(c.Title, "持有")
                        //    || RegexTools.Contains(c.Title, "逢低", "关注")
                        //    || RegexTools.Contains(c.Title,"'推荐'")
                        //    || RegexTools.Contains(c.Title,"建议","参与")
                        //    || RegexTools.Contains(c.Title,"荐股")
                        //    || RegexTools.Contains(c.Title,"买入")
                        //    || RegexTools.Contains(c.Title,"增持")
                        //    || RegexTools.Contains(c.Title, "减持")
                        //    || RegexTools.Contains(c.Title, "介入")
                        //    || RegexTools.Contains(c.Title, "技术", "形态")
                        //    || RegexTools.Contains(c.Title, "均线", "多头")
                        //    || RegexTools.Contains(c.Title, "券商评级")
                        //    || RegexTools.Contains(c.Title, "卖出")
                        //    )
                        //{
                        //    continue;
                        //}
                    }
                    if (!string.IsNullOrEmpty(c.Content))
                    {
                        c.Content = StringTools.TxtClear(c.Content);
                    }
                    lst1.Add(c);
                }
            }
        }



        public List<Em_Grab_GaoTime_Zx> Get公告列表_New(string stock,int top, bool flagContent, bool flagPdf)
        {
            string key = "cmsservice.BLL.Em_Grab_GaoTime_ZxBLLGet公告列表_New." + stock+"."+top+"."+flagContent.ToString()+"."+flagPdf.ToString();
            News2CacheTools cache = new News2CacheTools();
            var lst = cache.Get<List<Em_Grab_GaoTime_Zx>>(key);
            if (lst == null)
            {
                Em_Grab_GaoTime_ZxDAL dal = new Em_Grab_GaoTime_ZxDAL();
                lst = new List<Em_Grab_GaoTime_Zx>();
                var lst2 = dal.Get公告_New(stock, top, false, flagPdf);
                if (flagContent)
                {
                    foreach (var c in lst2)
                    {
                        var gg= Get公告内容_New(c.NewsID.ToString());
                        c.Content = gg.Content;
                    }
                }
                Add(lst, lst2);
                cache.Add(key, lst, 0, 20, 0);
            }

            return lst;
        }

        public List<Em_Grab_GaoTime_Zx> Get公告列表_New(string stock)
        {
            News2CacheTools cache = new News2CacheTools();
            string key = "cmsservice.BLL.Em_Grab_GaoTime_ZxBLLGet公告列表1_New."+stock;
            var lst= cache.Get<List<Em_Grab_GaoTime_Zx>>(key);
            if (lst == null)
            {
                Em_Grab_GaoTime_ZxDAL dal = new Em_Grab_GaoTime_ZxDAL();
                lst = new List<Em_Grab_GaoTime_Zx>();
                var lst2 = dal.Get公告_New(stock, 0, false, true);
                Add(lst, lst2);
                cache.Add(key, lst, 0, 5, 0);
            }
            return lst;
        }

        public Em_Grab_GaoTime_Zx Get公告内容_New(string id)
        {
            string key = "cmsservice.BLL.Em_Grab_GaoTime_ZxBLLGet公告内容22_New."+id;
            News2CacheTools cache = new News2CacheTools();
            var c= cache.Get<Em_Grab_GaoTime_Zx>(key);
            if (c != null)
            {
                return c;
            }
            else
            {
                Em_Grab_GaoTime_ZxDAL dal = new Em_Grab_GaoTime_ZxDAL();
                c = dal.Get公告内容_New(id);
                if (c != null && !string.IsNullOrEmpty(c.Content))
                {
                    c.Content = StringTools.TxtClear(c.Content);
                    c.Content = StringTools.TextToHtml(c.Content);
                }
                if (c != null)
                {
                    cache.Add(key, c, 0, 10, 0);
                }
                return c;
            }
        }

        public Em_Grab_GaoTime_Zx GetNewsById(string newsId)
        {
            Em_Grab_GaoTime_ZxDAL dal = new Em_Grab_GaoTime_ZxDAL();
            var c = dal.GetNewsById(newsId);
            if (c == null)
            {
                c = Get新闻评论New_Nr(newsId);
            }
            if (c == null)
            {
                c= dal.Get新闻评论New2_Nr(newsId);
            }
            if (c != null)
            {
                if (!string.IsNullOrEmpty(c.Title))
                {
                    c.Title = StringTools.TxtClear(c.Title);
                    c.Title = StringTools.TextToHtml(c.Title);
                }
                if (!string.IsNullOrEmpty(c.Content))
                {
                    c.Content = StringTools.TxtClear(c.Content);
                    c.Content = StringTools.TextToHtml(c.Content);
                }
            }
            return c;
        }

        public Em_Grab_GaoTime_Zx GetNewsById_guid(string guid)
        {
            Em_Grab_GaoTime_ZxDAL dal = new Em_Grab_GaoTime_ZxDAL();
            var c = dal.GetNewsById_Guid(guid);
            if (c == null)
            {
                c = dal.Get新闻评论New_Nr(guid);
            }
            if (c == null)
            {
                c = dal.Get新闻评论New2_Nr(guid);
            }
            if (c != null)
            {
                if (!string.IsNullOrEmpty(c.Title))
                {
                    c.Title = StringTools.TxtClear(c.Title);
                    c.Title = StringTools.TextToHtml(c.Title);
                }
                if (!string.IsNullOrEmpty(c.Content))
                {
                    c.Content = StringTools.TxtClear(c.Content);
                    c.Content = StringTools.TextToHtml(c.Content);
                }
            }
            return c;
        }

        public List<Em_Grab_GaoTime_Zx> Get新闻评论New(string stock)
        {
            Em_Grab_GaoTime_ZxDAL dal = new Em_Grab_GaoTime_ZxDAL();
            var lst= dal.Get新闻评论New(stock,200,false);
            var lst2 = dal.Get新闻评论(stock, 0, false);
            lst.AddRange(lst);
            return lst.OrderByDescending(x => x.ReportTime).ToList();
        }

        public Em_Grab_GaoTime_Zx Get新闻评论New_Nr(string newsid)
        {
            Em_Grab_GaoTime_ZxDAL dal = new Em_Grab_GaoTime_ZxDAL();

            if (!string.IsNullOrEmpty(newsid))
            {
                return dal.Get新闻评论New_Nr(newsid);
            }
            else
            {
                return null;
            }
        }
    }
}
